﻿using System;
using System.Diagnostics.Contracts;
using Adalbertus.BudgetPlanner.Domain;

namespace Adalbertus.BudgetPlanner.ViewModels.ExternalSources
{
    public class SavingEntryVM : UnitOfWorkVM
    {
        private SavingEntry _wrappedSavingEntry;

        public SavingEntry WrappedSavingEntry
        {
            get
            {
                Contract.Ensures(Contract.Result<SavingEntry>() != null);
                return _wrappedSavingEntry;
            }
        }

        private SavingVM _parent;
        public SavingVM Parent
        {
            get
            {
                Contract.Ensures(Contract.Result<SavingVM>() != null);
                return _parent;
            }
        }

        public bool IsWithdrawPresentationDisabled { get; set; }

        public SavingEntryVM(SavingEntry savingEntry) :
            this(savingEntry, null)
        { }

        public SavingEntryVM(SavingEntry savingEntry, SavingVM parent)
        {
            Contract.Requires<ArgumentNullException>(savingEntry != null);
            _wrappedSavingEntry = savingEntry;
            _parent = parent;
        }

        public decimal Value
        {
            get 
            {
                decimal value = _wrappedSavingEntry.Value;
                if (IsWithdrawPresentationDisabled)
                {
                    if(value < 0)
                    {
                        value *= -1;
                    }
                }
                return value;
            }
            set
            {
                _wrappedSavingEntry.Value = value;
                NotifyOfPropertyChange(() => Value);
            }
        }

        public DateTime Date
        {
            get { return _wrappedSavingEntry.Date; }
            set
            {
                _wrappedSavingEntry.Date = value;
                NotifyOfPropertyChange(() => Date);
            }
        }

        public string Name
        {
            get { return _wrappedSavingEntry.Saving.Name;  }
            set
            {
                _wrappedSavingEntry.Saving.Name = value;
                NotifyOfPropertyChange(() => Name);
            }
        }

        public string Budget
        { 
            get
            {
                if (_wrappedSavingEntry.Budget != null)
                {
                    return _wrappedSavingEntry.Budget.DateFrom.ToString("YYYY-MM");
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    }
}
